package com.jxc.action.kcgl;

import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jxc.action.BaseAction;
import com.jxc.model.po.Checklist;
import com.jxc.model.po.Goods;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.SessionInfo;
import com.jxc.model.vo.VChecklist;
import com.jxc.model.vo.VChecklistItem;
import com.jxc.model.vo.VGoods;
import com.jxc.model.vo.VRepertory;
import com.jxc.model.vo.VSerial;
import com.jxc.model.vo.VUser;
import com.jxc.service.ChecklistItemServiceI;
import com.jxc.service.ChecklistServiceI;
import com.jxc.service.GoodsServiceI;
import com.jxc.service.RepertoryServiceI;
import com.jxc.service.SerialService;
import com.jxc.service.UserServiceI;
import com.jxc.util.ResourceUtil;
import com.jxc.util.SerialUtils;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 库存调拨明细
 * 
 * @author Tino
 * 
 */
@Namespace("/kcgl")
@Action(value = "goodsCheckItemAction", results = { @Result(name = "goodsCheckItemAdd", location = "/amountRelate/goodsCheckItem.jsp"),
		@Result(name = "goodsCheckItemEdit", location = "/amountRelate/goodsCheckItem.jsp"),
		@Result(name = "goodsCheckRepertory", location = "/amountRelate/goodsCheckRepertory.jsp") })
public class GoodsCheckItemAction extends BaseAction implements ModelDriven<VChecklistItem> {

	private VChecklistItem model = new VChecklistItem();

	@Autowired
	private ChecklistServiceI checklistService;
	@Autowired
	private ChecklistItemServiceI checklistItemService;
	@Autowired
	private RepertoryServiceI repertoryService;
	@Autowired
	private GoodsServiceI goodsService;
	@Autowired
	private UserServiceI userService;
	@Autowired
	private SerialService SerialService;


	public void noAuth_datagrid() {
		VChecklist vcl = new VChecklist();
		vcl.setId(model.getClid());
		List<VChecklist> vcls = checklistService.combobox(vcl);
		DataGrid dg = checklistItemService.datagrid(model);
		List<VChecklistItem> vclis = dg.getRows();
		for (VChecklistItem vcli : vclis) {
			vcli.setRid(vcls.get(0).getRid());
			vcli.setRname(vcls.get(0).getRname());
		}
		this.writeJson(dg);
	}

	public String goodsCheckItemAdd() {
		try {
			SessionInfo sessionInfo = (SessionInfo) ServletActionContext.getRequest().getSession().getAttribute(ResourceUtil.getSessionInfoName());
			VUser vuser = userService.get(sessionInfo.getUserId());
			String rid = this.getRequest().getParameter("rid");
			/*
			 * 查询当前盘点的仓库
			 */
			VRepertory vr = new VRepertory();
			vr.setId(Long.parseLong(rid));
			vr = repertoryService.list(vr).get(0);
			this.getRequest().setAttribute("rname", vr.getName());
			/*
			 * 创建盘点单
			 */
			VChecklist vcl = new VChecklist();
			vcl.setRid(Long.parseLong(rid));
			vcl.setUid(vuser.getId());
			vcl.setDate(new Date());
			vcl.setIsCheckOver(false);
			VSerial vs = new VSerial();
			vs.setRid(Integer.parseInt(rid));//库存盘点单号
			vs.setType("PD");
			vcl.setSerial(SerialService.getAll(vs));
			Long clid = checklistService.save(vcl);
			this.getRequest().setAttribute("clid", clid);
			/*
			 * 查询当前仓库中的所有未删除的商品
			 */
			VGoods vg = new VGoods();
			vg.setRid(Long.parseLong(rid));
			vg.setUid(sessionInfo.getUserId());
			List<VGoods> vgoodz = goodsService.findByOrid(vg);
			/*
			 * 根据查询出来的商品创建盘点单明细
			 */
			for (VGoods vgoods : vgoodz) {
				VChecklistItem vcli = new VChecklistItem();
				vcli.setGid(vgoods.getId());
				vcli.setGname(vgoods.getName());
				vcli.setInitAmount(new Long(0));
				vcli.setOrderTotal(vgoods.getOrderTotal());
				vcli.setOrderReturnTotal(vgoods.getOrderReturnTotal());
				vcli.setSaleTotal(vgoods.getSaleTotal());
				vcli.setSaleReturnTotal(vgoods.getSaleReturnTotal());
				vcli.setRepertoryTotal(vgoods.getRepertoryTotal());
				vcli.setDamageTotal(vgoods.getDamageTotal());
				vcli.setCheckAmount(new Long(0));
				vcli.setBeforeCheck(vgoods.getRepertoryTotal());
				vcli.setIsChecked(false);
				vcli.setClid(clid);
				vcli.setRid(Long.parseLong(rid));
				vcli.setRname(vr.getName());
				checklistItemService.save(vcli);

//					private Goods goods;
//					private Integer checkAmount;
//					private Boolean isChecked;
//					private Date checkDate;
//					private Checklist checklist;
//					private Integer initAmount;
//					private Long orderTotal;
//					private Long orderReturnTotal;
//					private Long saleTotal;
//					private Long saleReturnTotal;
//					private Long damageTotal;
//					private Long transferTotal;
//					private Long repertoryTotal;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "goodsCheckItemAdd";
	}

	public String noAuth_goodsCheckRepertory() {
		return "goodsCheckRepertory";
	}

	public String goodsCheckItemEdit() {
		VRepertory vr = new VRepertory();
		vr.setId(model.getRid());
		vr = repertoryService.list(vr).get(0);
		this.getRequest().setAttribute("rname", vr.getName());
		this.getRequest().setAttribute("clid", model.getId());
		return "goodsCheckItemEdit";
	}

	@Override
	public VChecklistItem getModel() {
		return model;
	}

}
